﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Reflection;
namespace LotteryAdmin
{
    public partial class ChangeBank : ChildWindow
    {
        LotteryAdminClient srv;
        public ChangeBank()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.UpdateBankCompleted += new EventHandler<UpdateBankCompletedEventArgs>(srv_UpdateBankCompleted);
            this.dataForm1.Loaded += new RoutedEventHandler(dataForm1_Loaded);
        }

        void dataForm1_Loaded(object sender, RoutedEventArgs e)
        {
            if (orgBank != null)
            {
                if (bank.BankType.Trim() == "财付通")
                    (this.dataForm1.FindNameInContent("cbx_bankType") as ComboBox).SelectedIndex = 1;
                else
                    (this.dataForm1.FindNameInContent("cbx_bankType") as ComboBox).SelectedIndex = 0;
            }
        }
        void srv_UpdateBankCompleted(object sender, UpdateBankCompletedEventArgs e)
        {
            if (e.Result)
            {
                orgBank = ModelCopy.CopyValueForModel<CarrierBankInfo>(bank, orgBank);
                this.DialogResult = true;
            }
            else
                MessageBox.Show("操作失败,网络异常或者服务器不可连接!");
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataForm1.ValidateItem())
            {
                bank.BankType = ((this.dataForm1.FindNameInContent("cbx_bankType") as ComboBox).SelectedItem as ComboBoxItem).Content.ToString();
                srv.UpdateBankAsync(bank);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        public void ShowAddWindow()
        {
            this.Title = "平台银行资料新增";
            bank = new CarrierBankInfo();
            this.dataForm1.CurrentItem = bank;
            base.Show();
        }
        CarrierBankInfo bank;
        CarrierBankInfo orgBank;
        public void ShowUpdateWindow(CarrierBankInfo info)
        {
            this.Title = "平台银行资料修改";
            orgBank = info;
            bank = ModelCopy.CopyValueForModel<CarrierBankInfo>(info);
            this.dataForm1.CurrentItem = bank;
            base.Show();
        }
        CarrierBankInfo CopyValueForModel(CarrierBankInfo org)
        {
            CarrierBankInfo dest = new CarrierBankInfo();
            foreach (PropertyInfo po in org.GetType().GetProperties())
            {
                foreach (PropertyInfo pd in dest.GetType().GetProperties())
                {
                    if (pd.Name == po.Name)
                    {
                        pd.SetValue(dest, po.GetValue(org, null), null);
                        break;
                    }
                }
            }
            return dest;
        }
    }
}

